console.log("我是项目发明人基本信息页面");


layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element'], function() {
	var laydate = layui.laydate;
	form = layui.form;
	upload = layui.upload;
	layer = layui.layer;
	laytpl = layui.laytpl;
	element = layui.element;

	// 监听提交
	/*form.on('submit(formDemo)', function(data) {
		layer.msg(JSON.stringify(data.field));
		return false;
	});*/

	layui.code();

	// 时间选择
	laydate.render({
		elem: '.startDate' //指定元素
	});
// 时间选择
	laydate.render({
		elem: '.endDate' //指定元素
	});

	$("#submitNewEducation").click(function () {
		// 判断必填项--提示--统校验
		var params = {};

		// 集合所有参数
		$("#addNewEducationForm .save_val").each(function() {
			var name = $(this).attr("name");
			var val = $(this).val();
			params[name] = val;
		});

		if(!params.startDate){
			layer.msg("开始时间不能为空");
			return false;
		}
		// if(!params.endDate){
		// 	layer.msg("结束时间不能为空");
		// 	return false;
		// }
		if(!params.institute){
			layer.msg("学校名称不能为空");
			return false;
		}
		if(!params.speciality){
			layer.msg("专业不能为空");
			return false;
		}
		if(!params.degree){
			layer.msg("请选择教育程度");
			return false;
		}

		if(params.startDate && params.endDate && params.startDate > params.endDate){
			layer.msg("开始时间不能晚于结束时间");
			return false;
		}

		var url = cgiMain.academistEduAdd;
		if(params.id && params.id > 0){
			url = cgiMain.academistEduEdit;
		}


		AJAX.post(url,
			params,
			function (res) {
				if(res.code=='0000'){
					layer.msg('保存成功');
					$("#myModal").hide();
					window.location.reload();
				}
			});
		return false;
	});



	function  setSelect(dom,data){
		for(let item in data){
			$(dom).append("<option value='" + item +"'>"+ data[item] +"</option>")
		}
		form.render('select');
	}


//获取学位
	AJAX.query(cgiMain.getDict,
		{type:6},
		function (res) {
			if(res.code=='0000'){
				$(".educationList").data("degreeList",res.result);
				setSelect($(".degree"), res.result);
				form.render('select');
			}
		});
// 添加教育经验
	$(".add_btn").on("click", function() {
		$("#myModal").show();
		$("#myModal input, #myModal textarea").val("");
	});




	//初始化列表
	setTimeout(function () {
		AJAX.post(cgiMain.academistEduInfo,
			{},
			function (res) {

				if(res.code == '0000'){
					$(".educationList").data("educationList",res.result);
					var tpl = $("#educationTpl").html();
					var degreeList = $(".educationList").data("degreeList");
					for(var i = 0; i < res.result.length;i++){
						res.result[i].degreeName = degreeList[res.result[i].degree];
						laytpl(tpl).render(res.result[i],function (html) {
							$(".educationList").append(html);
							$(".educationList").on("click",'.editEducation',function (elem) {
								editEducation(elem);

							});
							$(".educationList").on("click",'.deleteEducation',function (elem) {
								deleteEducation(elem);
							});
						});
					}
					element.render(".educationList");
				}

			});

	},500);


	function editEducation(dom){

		var educationId = $(dom.target).attr("data-id");
		var educationList = $(".educationList").data("educationList");

		var applicantEducation={};
		for(var i = 0 ; i < educationList.length;i++){
			if(educationList[i].id == educationId){
				applicantEducation = educationList[i];
				break;
			}
		}

		$("input[name='id']").val(applicantEducation.id);
		$(".startDate").val(applicantEducation.startDate);
		$(".endDate").val(applicantEducation.endDate);
		$(".speciality").val(applicantEducation.speciality);
		$(".institute").val(applicantEducation.institute);

		$("#myModal").show();
	}

	function deleteEducation(dom){

		var educationId = $(dom.target).attr("data-id");

		layer.open({
			content: '确定删除吗?'
			,btn: ['删除', '取消']
			,yes: function(index, layero){
				//按钮【按钮一】的回调
				AJAX.post(cgiMain.academistEdudelete,
					{id:educationId},
					function (res) {
						if(res.code=='0000'){
							layer.msg('删除成功');
							window.location.reload();
						}
					});


			}
			,btn2: function(index, layero){
				//按钮【按钮二】的回调
			}

			,cancel: function(){
				//右上角关闭回调
			}
		});


	}


	$(".modal-footer > .layui-btn-primary").on("click",function () {
		$("#myModal").hide();
		$("#myModal input, #myModal textarea").val("");
	});

});





